package com.clkj.aimin.controller;
import com.clkj.aimin.pojo.doctor.*;
import com.clkj.aimin.pojo.common.Result;
import com.clkj.aimin.pojo.common.StatusCode;
import com.clkj.aimin.pojo.user.User;
import com.clkj.aimin.pojo.user.UserOrder;
import com.clkj.aimin.pojo.user.UserRegister;
import com.clkj.aimin.service.doctor.DoctorService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author yawang
 * @create 2024/04/02 11:50:12
 **/
@RestController
@RequestMapping("/doctor")
@CrossOrigin
@Api(value = "/doctor", tags = {"医生信息api"})

public class DoctorController {
    @Autowired
    private DoctorService doctorService;
    @ApiOperation(value = "后台---添加医生信息")
    @PostMapping("/addDoctor")
    public Result addDoctor(Doctor doctor, HttpServletRequest request) {
        try {

            doctorService.addDoctor(doctor,request);
            return new Result(true, StatusCode.OK, "添加成功", null);
        } catch (Exception e) {
            return new Result(false, StatusCode.ERROR, "添加失败", null);
        }
    }

    @ApiOperation(value = "添加科室信息")
    @PostMapping("/addDepartment")
    public Result addDepartment(Department department) {
        try {
            doctorService.addDepartment(department);
            return new Result(true, StatusCode.OK, "添加成功", null);
        } catch (Exception e) {
            return new Result(false, StatusCode.ERROR, "添加失败", null);
        }
    }

    @ApiOperation(value = "获取科室信息")
    @PostMapping("/listDepartment")
    public Result listDepartment() {
        try {
            List<Department> departmentList = doctorService.listDepartment();
            return new Result(true, StatusCode.OK, "添加成功", departmentList);
        } catch (Exception e) {
            return new Result(false, StatusCode.ERROR, "添加失败", null);
        }
    }

    @ApiOperation(value = "app --获取医生列表")
    @PostMapping("/listDoctors")
    public Result listDoctors() {
        try {
            List<User> doctors = doctorService.listDoctors();
            return new Result(true, StatusCode.OK, "添加成功", doctors);
        } catch (Exception e) {
            return new Result(false, StatusCode.ERROR, "添加失败", null);
        }
    }

//    @ApiOperation(value = "获取医生当前选择时间的号")
//    @PostMapping("/listDoctorTimes")
//    public Result listDoctorTimes(Integer doctorId,Integer weekNum,@ApiParam(name = "dateTime", value = "当天的时间 2024-03-24") @RequestParam String dateTime,
//                                  Integer userId) {
//        try {
//            Map map = doctorService.listDoctorTimes(doctorId,weekNum,dateTime,userId);
//            return new Result(true, StatusCode.OK, "添加成功", map);
//        } catch (Exception e) {
//            return new Result(false, StatusCode.ERROR, "添加失败", null);
//        }
//    }
//
//    @ApiOperation(value = "添加医生出诊的时间段")
//    @PostMapping("/addDoctorTimes")
//    public Result addDoctorTimes(@ApiParam(name = "doctorId", value = "医生ID") @RequestParam("doctorId") Integer doctorId,
//                                 @ApiParam(name = "weekNum", value = "周几出诊，例如1,2,3") @RequestParam("weekNum") String weekNum,
//                                 @ApiParam(name = "settingNum", value = "每个号的数量") @RequestParam("settingNum") Integer settingNum,
//                                 @ApiParam(name = "timeSlot", value = "时间段 用，分割，例如8:30-9:00,9:00-9:30") @RequestParam("timeSlot") String timeSlot,
//                                 @ApiParam(name = "flag", value = "1为添加 0为修改") @RequestParam("flag") Integer flag) {
//        try {
//            doctorService.addDoctorTimes(doctorId,weekNum,settingNum,timeSlot,flag);
//            return new Result(true, StatusCode.OK, "添加成功");
//        } catch (Exception e) {
//            return new Result(false, StatusCode.ERROR, "添加失败", null);
//        }
//    }
//    @ApiOperation(value = "添加用户挂号记录")
//    @PostMapping("/addUserRegister")
//    public Result addUserRegister(@ApiParam(name = "doctorId", value = "医生ID") @RequestParam("doctorId") Integer doctorId,
//                                 @ApiParam(name = "weekNum", value = "周几1,2,3") @RequestParam("weekNum") Integer weekNum,
//                                  @ApiParam(name = "userId", value = "用户ID") @RequestParam("userId") Integer userId,
//                                  @ApiParam(name = "timeSlot", value = "时间段") @RequestParam("timeSlot") String timeSlot,
//                                  @ApiParam(name = "registerDate", value = "哪天的号,2024-04-03") @RequestParam("registerDate") String registerDate,
//                                  @ApiParam(name = "departmentId", value = "科室ID") @RequestParam("departmentId") Integer departmentId) {
//        try {
//            doctorService.addUserRegister(doctorId,weekNum,userId,timeSlot,registerDate,departmentId);
//            return new Result(true, StatusCode.OK, "添加成功");
//        } catch (Exception e) {
//            return new Result(false, StatusCode.ERROR, "添加失败", null);
//        }
//    }
//
//    @ApiOperation(value = "获取用户挂号记录")
//    @PostMapping("/listUserRegister")
//    public Result listUserRegister(@ApiParam(name = "userId", value = "用户ID") @RequestParam("userId") Integer userId) {
//        try {
//            List<UserRegister> userRegisters = doctorService.listUserRegister(userId);
//            return new Result(true, StatusCode.OK, "添加成功",userRegisters);
//        } catch (Exception e) {
//            return new Result(false, StatusCode.ERROR, "添加失败", null);
//        }
//    }
//
//    @ApiOperation(value = "取消用户挂号记录")
//    @PostMapping("/cancelUserRegister")
//    public Result cancelUserRegister(@ApiParam(name = "registerId", value = "记录ID") @RequestParam("registerId") Integer registerId) {
//        try {
//            doctorService.cancelUserRegister(registerId);
//            return new Result(true, StatusCode.OK, "添加成功");
//        } catch (Exception e) {
//            return new Result(false, StatusCode.ERROR, "添加失败", null);
//        }
//    }

//    @ApiOperation(value = "根据科室获取医生")
//    @PostMapping("/listDoctorByDepart")
//    public Result listDoctorByDepart(@ApiParam(name = "departmentId", value = "科室ID") @RequestParam("departmentId") Integer departmentId) {
//        try {
//            List<Doctor> doctors =  doctorService.listDoctorByDepart(departmentId);
//            return new Result(true, StatusCode.OK, "添加成功",doctors);
//        } catch (Exception e) {
//            return new Result(false, StatusCode.ERROR, "添加失败");
//        }
//    }

//    @ApiOperation(value = "获取医生预约号信息")
//    @PostMapping("/listAllDoctorTimers")
//    public Result listAllDoctorTimers() {
//        try {
//            List<Doctor> doctors =  doctorService.listAllDoctorTimers();
//            return new Result(true, StatusCode.OK, "添加成功",doctors);
//        } catch (Exception e) {
//            return new Result(false, StatusCode.ERROR, "添加失败");
//        }
//    }

    @ApiOperation(value = "app---兑换用户身份")
    @PostMapping("/updateUserType")
    public Result updateUserType(Integer  userId,String code,
                                 @ApiParam(name="flag",value="2为医助，1为医生") @RequestParam("flag") Integer flag,
                                 @ApiParam(name="doctorId",value="医助需要绑定的医生id") @RequestParam("doctorId") Integer doctorId) {
        try {
            Map map=  doctorService.updateUserType(userId,code,flag,doctorId);
            return new Result(true, StatusCode.OK, "添加成功",map);
        } catch (Exception e) {
            return new Result(false, StatusCode.ERROR, "添加失败");
        }
    }

    @ApiOperation(value = "医生更新个人信息")
    @PostMapping("/updateDoctor")
    public Result updateDoctor(Doctor doctor,HttpServletRequest request) {
        try {
            Map map=  doctorService.updateDoctor(doctor,request);
            return new Result(true, StatusCode.OK, "添加成功",map);
        } catch (Exception e) {
            return new Result(false, StatusCode.ERROR, "添加失败");
        }
    }

    @ApiOperation(value = "app--我的患者(医生、助理)")
    @PostMapping("/listUsersByDoctor")
    public Result listUsersByDoctor(Integer userId,Integer userType,
                                    @ApiParam(name="userName",value="搜索名，默认传null") String userName) {
        try {
            List<User> users =  doctorService.listUsersByDoctor(userId,userType,userName);
            return new Result(true, StatusCode.OK, "添加成功",users);
        } catch (Exception e) {
            return new Result(false, StatusCode.ERROR, "添加失败");
        }
    }

//    @ApiOperation(value = "我的预约列表（医生）")
//    @PostMapping("/listUserRegisterByDoctor")
//    public Result listUserRegisterByDoctor(Integer doctorId) {
//        try {
//            List<UserRegister> users =  doctorService.listUserRegisterByDoctor(doctorId);
//            return new Result(true, StatusCode.OK, "添加成功",users);
//        } catch (Exception e) {
//            return new Result(false, StatusCode.ERROR, "添加失败");
//        }
//    }

    @ApiOperation(value = "app----我的专属二维码（医生 ）")
    @PostMapping("/listErweima")
    public Result listErweima(Integer doctorId) {
        try {
            String userImg =  doctorService.listErweima(doctorId);
            return new Result(true, StatusCode.OK, "添加成功",userImg);
        } catch (Exception e) {
            return new Result(false, StatusCode.ERROR, "添加失败");
        }
    }
//    @ApiOperation(value = "给医生绑定助理")
//    @PostMapping("/bandingHelper")
//    public Result bandingHelper(Integer doctorId, Integer helperId) {
//        try {
//              doctorService.bandingHelper(doctorId,helperId);
//            return new Result(true, StatusCode.OK, "添加成功");
//        } catch (Exception e) {
//            return new Result(false, StatusCode.ERROR, "添加失败");
//        }
//    }
    @ApiOperation(value = "app--获取所有助理信息")
    @PostMapping("/listHelper")
    public Result listHelper() {
        try {
            List<User> users =  doctorService.listHelper();
            return new Result(true, StatusCode.OK, "添加成功",users);
        } catch (Exception e) {
            return new Result(false, StatusCode.ERROR, "添加失败");
        }
    }

    @ApiOperation(value = "后台获取医生列表")
    @PostMapping("/listAllDoctors")
    public Result listAllDoctors(Integer page,Integer size,String userName) {
        try {
            PageInfo doctors = doctorService.listAllDoctors(page,size,userName);
            return new Result(true, StatusCode.OK, "添加成功", doctors);
        } catch (Exception e) {
            return new Result(false, StatusCode.ERROR, "添加失败", null);
        }
    }

    @ApiOperation(value = "后台医生列表启用禁用")
    @PostMapping("/updateDoctorStatus")
    public Result updateDoctorStatus(String ids,Integer status) {
        try {
             doctorService.updateDoctorStatus(ids,status);
            return new Result(true, StatusCode.OK, "更新成功");
        } catch (Exception e) {
            return new Result(false, StatusCode.ERROR, "更新失败", null);
        }
    }

    @ApiOperation(value = "后台登录")
    @PostMapping("/backLogin")
    public Result backLogin(String managerAccount,String managerPass) {
        try {
            Map  map =  doctorService.backLogin(managerAccount,managerPass);
            return new Result(true, StatusCode.OK, "成功",map);
        } catch (Exception e) {
            return new Result(false, StatusCode.ERROR, "失败", null);
        }
    }


    @ApiOperation(value = "app--实时聊天")
    @PostMapping("/senMessage")
    public Result senMessage(Integer userId,Integer doctorId,String message) {
        try {
            Map map = doctorService.senMessage(userId,doctorId,message);
            return new Result(true, StatusCode.OK, "成功",map);
        } catch (Exception e) {
            return new Result(false, StatusCode.ERROR, "失败", null);
        }
    }

    @ApiOperation(value = "app--医生端查看当天就医申请列表")
    @PostMapping("/listCurrApply")
    public Result listCurrApply(Integer doctorId) {
        try {
            List<UserOrder> orderList = doctorService.listCurrApply(doctorId);
            return new Result(true, StatusCode.OK, "成功",orderList);
        } catch (Exception e) {
            return new Result(false, StatusCode.ERROR, "失败", null);
        }
    }
}
